package cc.rengu.igas.cops.biz.service.impl;

import cc.rengu.igas.cops.common.enums.CopsRespCodeEnum;
import cc.rengu.igas.cops.facade.OpmsOutSideService;
import cc.rengu.igas.cops.facade.request.TokenPreSignQueryRequest;
import cc.rengu.igas.cops.facade.request.TokenPreSignRequest;
import cc.rengu.igas.cops.facade.request.TokenResSignInfoRequest;
import cc.rengu.igas.cops.facade.response.TokenPreSignQueryResponse;
import cc.rengu.igas.cops.facade.response.TokenPreSignResponse;
import cc.rengu.igas.cops.facade.response.TokenResSignInfoResponse;
import cc.rengu.igas.cops.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

public class OpmsOutSideServiceImpl implements OpmsOutSideService {
    RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<TokenPreSignResponse> tokenPreSign(TokenPreSignRequest tokenPreSignRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<TokenPreSignResponse> result = new Result<>();
        TokenPreSignResponse tokenPreSignResponse = new TokenPreSignResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(tokenPreSignRequest, TokenPreSignResponse.class);
            result.setResult((TokenPreSignResponse) dubboResult.getResult());
            result.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("opms-银联无跳转预签约失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                tokenPreSignResponse.setRespDesc(((BizException) e).getCode());
                tokenPreSignResponse.setRespDesc(e.getMessage());
            } else {
                tokenPreSignResponse.setRespCode(CopsRespCodeEnum.TRANS_FAIL.getRespCode());
                tokenPreSignResponse.setRespDesc(CopsRespCodeEnum.TRANS_FAIL.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<TokenPreSignQueryResponse> tokenPreSignQuery(TokenPreSignQueryRequest tokenPreSignQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<TokenPreSignQueryResponse> result = new Result<>();
        TokenPreSignQueryResponse tokenPreSignQueryResponse = new TokenPreSignQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(tokenPreSignQueryRequest, TokenPreSignQueryResponse.class);
            result.setResult((TokenPreSignQueryResponse) dubboResult.getResult());
            result.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("opms-银联无跳转签约查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                tokenPreSignQueryResponse.setRespDesc(((BizException) e).getCode());
                tokenPreSignQueryResponse.setRespDesc(e.getMessage());
            } else {
                tokenPreSignQueryResponse.setRespCode(CopsRespCodeEnum.TRANS_FAIL.getRespCode());
                tokenPreSignQueryResponse.setRespDesc(CopsRespCodeEnum.TRANS_FAIL.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<TokenResSignInfoResponse> tokenResSignInfo(TokenResSignInfoRequest tokenResSignInfoRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<TokenResSignInfoResponse> result = new Result<>();
        TokenResSignInfoResponse tokenResSignInfoResponse = new TokenResSignInfoResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(tokenResSignInfoRequest, TokenResSignInfoResponse.class);
            result.setResult((TokenResSignInfoResponse) dubboResult.getResult());
            result.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("opms-银联无跳转签约查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                tokenResSignInfoResponse.setRespDesc(((BizException) e).getCode());
                tokenResSignInfoResponse.setRespDesc(e.getMessage());
            } else {
                tokenResSignInfoResponse.setRespCode(CopsRespCodeEnum.TRANS_FAIL.getRespCode());
                tokenResSignInfoResponse.setRespDesc(CopsRespCodeEnum.TRANS_FAIL.getRespDesc());
            }
        }
        return result;
    }

}
